package com.itheima.tanhua.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.tanhua.api.db.UserInfoServiceApi;
import com.itheima.tanhua.mapper.UserInfoMapper;
import com.itheima.tanhuan.pojo.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Stream;

@DubboService
public class UserInfoServiceApiImpl implements UserInfoServiceApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void saveUserInfo(UserInfo userInfo) {
        //INSERT INTO tb_user_info  ( nickname,   gender,   city, birthday,     created, updated )  VALUES  ( ?,   ?,   ?, ?,     ?, ? )
        userInfoMapper.saveUserInfo(userInfo);
    }


    @Override
    public void updateAvatar(UserInfo userInfo) {
        //update tb_user_info set avatar=? where id=?
        userInfoMapper.updateById(userInfo);
    }

    /**
     * userInfoServiceApi
     *
     * @param userId
     * @return
     */
    @Override
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    //根据id查相应的数据
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, Object o) {
        Map<Long, UserInfo> map = new TreeMap<>();
        for (Long id : ids) {
            UserInfo userInfo = userInfoMapper.selectById(id);
            map.put(id, userInfo);
        }
        return map;
    }

    @Override
    public List<UserInfo> findByFriendIds(List<Long> friendIds, String keyName) {
        LambdaQueryWrapper<UserInfo>wrapper=new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(keyName),UserInfo::getNickname,keyName);
        wrapper.in(UserInfo::getId,friendIds);
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        return userInfos;

    }




}
